package com.blankii.androidlibrary.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.google.android.material.snackbar.Snackbar

/**
 * author: blankii
 * created on: 2024/4/16 17:22
 * description: fragment 的父类
 */
abstract class BaseBindingFragment<VB : ViewBinding>(
    private val inflate: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : Fragment() {

    private var _binding: VB? = null
    val binding: VB get() = _binding!!
    val TAG: String
        get() = this::class.simpleName!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onCreateInit()
        lifecycle.addObserver(LifecycleObserver("${this::class.simpleName!!}"))
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        _binding = inflate(inflater, container, false)
//        "baseFragment-onCreateView".loge("lifecycle")
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        onInitView()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    abstract fun onCreateInit()

    abstract fun onInitView()

    fun String.showSnackBar() {
        requireActivity().runOnUiThread {
            Snackbar.make(requireActivity(), binding.root, this, 2000).show()
        }
    }
}